var game = new Phaser.Game(640,1080,Phaser.CANVAS,'game');
var score = 0, time = 0, speeds =2000, game_bg = null, speedX = 10, speedY = 10, timeLimit;
var gameBoot = {
	preload: function ( ){
		this.game.stage.backgroundColor = '#F66256';
		this.input.maxPointers = 1;
		if(!game.device.desktop){
      		this.stage.forcePortrait = true;
      		this.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT;
  		}else{
      		this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  		}
  		this.scale.pageAlignHorizontally = true;
 		this.scale.pageAlignVertically = true;
  		this.scale.setScreenSize(true);
  		this.scale.refresh();
		game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.scale.pageAlignHorizontally = true;
		game.load.image('gameload', ROOT+'images/gameload.png');
		game.load.image('logo', ROOT+'images/logo.png');
		game.load.image('bottom', ROOT+'images/imgbottom.png');
		game.load.image('game_bg', ROOT+'images/bg.png');
		game.load.image('gameimg', ROOT+'images/gameimg.png');
		game.load.image('sd', ROOT+'images/gamecs.png');
		game.load.image('gf1', ROOT+'images/gf1.png');
		game.load.image('gf11', ROOT+'images/gf1-1.png');
		game.load.image('gf2', ROOT+'images/gf2.png');
		game.load.image('gf22', ROOT+'images/gf22.png');
		game.load.image('boom1', ROOT+'images/boom1.png');
		game.load.image('boom2', ROOT+'images/boom2.png');
		game.load.image('over_bg', ROOT+'images/overbg.png');
		game.load.image('btn6', ROOT+'images/btn6.png');
		game.load.image('btn7', ROOT+'images/btn7.png');
		game.load.image('time_proc', ROOT+'images/poce.png');//时间进度条
		game.load.image('sec', ROOT+'images/pocen.png');//进度块
		game.load.image('time', ROOT+'images/game-icon.png');//进度块
	},

	create: function( ){
		loading_pic = game.add.image(0,0,'loading_pic');
		loading_pic.anchor.set(0,0);
		loading_pic.inputEnabled = true;
		game.state.start('preload');
	},
}
var gamePreload ={
	create: function( ){
		game_go = game.add.image(0,0,'gameload');
		game_go.anchor.set(0,0);
		game_go.inputEnabled = true;
		game_go.events.onInputDown.addOnce(function(){
			game.state.start('start');
		});
	}
}
var gameStart = {
	create: function( ){
		$.ajax({
			url: 'index.php?g=mobile&m=index&a=checkAgain',
			type: 'GET',
			dataType: 'json',
			data: {
				wxid: wxid,
				openid: openid
			},
			success: function(res){
				if(res.status == 1)
				{
					
				}
				else
				{
					location.href = "index.php?g=mobile&m=index&a=index";
					return false;
				}
			}
		});
		timeLimit = 30;
		game.physics.startSystem(Phaser.Physics.ARCADE);
		game_bg = game.add.tileSprite(0,0,game.width,game.height,'game_bg');
		game.add.image(0, 900,'bottom');
		this.sd_pic = game.add.sprite(540,25,'logo');
		this.sd_pic = game.add.sprite(game.world.centerX,game.world.bounds.bottom,'sd');
		this.sd_pic.anchor.setTo(0.5,1.0);
		this.sd_pic.inputEnabled = true;
		this.sd_pic.input.enableDrag();
		this.sd_pic.input.allowVerticalDrag = false;
		game.physics.arcade.enable(this.sd_pic);
		this.sd_pic.body.collideWorldBounds = true;

		game.add.image(25, 25, 'time');//时间进度条
		game.add.image(105, 25, 'time_proc');//时间进度条
		this.timeCount = game.add.text(58, 60, '30', {
			font: '30px arial',
			fill: 'white'
		});
		this.timeCount.anchor.setTo(0.5,0.5);
		this.secBar = game.add.image(122, 57, 'sec');//添加进度块
		this.secBar.scale.setTo(26, 1);
		this.secBar.anchor.setTo(0,0);

		this.gf1 = game.add.group();
		this.gf1.enableBody = true;
		this.gf1.createMultiple(5, 'gf1'); 
		this.timer = this.game.time.events.loop(1350, this.addgoodnum, this, this.gf1);

		this.gf11 = game.add.group();
		this.gf11.enableBody = true;
		this.gf11.createMultiple(5, 'gf11'); 
		this.timer = this.game.time.events.loop(1400, this.addgoodnum, this, this.gf11);

		this.gf2 = game.add.group();
		this.gf2.enableBody = true;
		this.gf2.createMultiple(5, 'gf2'); 
		this.timer = this.game.time.events.loop(1500, this.addgoodnum, this, this.gf2);

		this.gf22 = game.add.group();
		this.gf22.enableBody = true;
		this.gf22.createMultiple(5, 'gf22'); 
		this.timer = this.game.time.events.loop(900, this.addgoodnum, this, this.gf22);

		this.boom1 = game.add.group();
		this.boom1.enableBody = true;
		this.boom1.createMultiple(10, 'boom1'); 
		this.timer = this.game.time.events.loop(1300, this.addgoodnum, this, this.boom1);

		this.boom2 = game.add.group();
		this.boom2.enableBody = true;
		this.boom2.createMultiple(10, 'boom2'); 
		this.timer = this.game.time.events.loop(1300, this.addgoodnum, this, this.boom2);

		this.timer = game.time.events.loop(1000, this.ticktok, this);
		game.add.image(0, 100,'gameimg');

	},
	update:function( ){
		game.physics.arcade.overlap(this.sd_pic, this.gf1, this.add_score15, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.gf11, this.add_score15, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.gf2, this.add_score10, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.gf22, this.add_score10, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.boom1, this.reduce_score5, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.boom2, this.reduce_score5, null, this);
	},
	addGoodnum: function (x, y,obj) {
		var goodnum = obj.getFirstDead();
		goodnum.anchor.setTo(0.5,0.5);
		goodnum.reset(x, y);
		goodnum.body.gravity.y =  this.rnd.integerInRange(100, 250);
		goodnum.body.velocity.y =  this.rnd.integerInRange(speedX, speedY);
		goodnum.checkWorldBounds = true;
		goodnum.outOfBoundsKill = true;
	},
	addgoodnum: function (obj) {
		this.addGoodnum(this.rnd.integerInRange(120, 550), 240, obj);
	},
	add_score15: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score += 15;
		}
	},
	add_score10: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score += 10;
		}
	},
	reduce_score5: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score -= 5;
		}
	},
	// 倒计时
	ticktok: function () {
		timeLimit--;
		this.timeCount.text = timeLimit;
		this.secBar.scale.setTo(26*(Math.round(timeLimit * 100 / 30)/100), 1);
		if (timeLimit === 0) {
			game.state.start('over');
		}
	}
}
var gameOver = {
	create: function( ){
		game.physics.startSystem(Phaser.Physics.ARCADE);
		game_bg = game.add.tileSprite(0,0,game.width,game.height,'game_bg');
		game.add.image(0, 900,'bottom');
		this.sd_pic = game.add.sprite(540,25,'logo');
		this.sd_pic = game.add.sprite(game.world.centerX,game.world.bounds.bottom,'sd');
		game.add.image(25, 25, 'time');//时间进度条
		game.add.image(105, 25, 'time_proc');//时间进度条
		this.timeCount = game.add.text(58, 60, '0', {
			font: '30px arial',
			fill: 'white'
		});
		this.timeCount.anchor.setTo(0.5,0.5);
		this.sd_pic.anchor.setTo(0.5,1.0);

		over_bg = game.add.image(game.world.centerX,game.world.centerY,'over_bg');
		over_bg.anchor.setTo(0.5,0.5);

		if(score == 0 ){
			total_score = game.add.text(game.width/2,game.height/2-40,'0',{font: '30px arial',fill: '#3a230a'});
		}
		else{
			total_score = game.add.text(game.width/2,game.height/2-40,'您的得分是'+score+'分',{font: '30px arial',fill: '#3a230a'});
		}
		total_score.anchor.setTo(0.5,0.5);

		btn6 = game.add.image(game.width/2,game.height/2+50,'btn6');
		btn7 = game.add.image(game.width/2,game.height/2+140,'btn7');
		btn6.anchor.setTo(0.5,0.5);
		btn6.inputEnabled = true;
		btn6.events.onInputDown.addOnce(function(){
			//location.href="./index.html?"+"rank";
			location.href="index.php?g=mobile&m=index&a=index&type=rank";
		});
		btn7.anchor.setTo(0.5,0.5);
		btn7.inputEnabled = true;
		btn7.events.onInputDown.addOnce(function(){
			game.state.start('start');
			//location.href="index.php?g=mobile&m=index&a=index&type=start";
		});
		$.ajax({
			url: 'index.php?g=mobile&m=index&a=sendScore',
			type: 'POST',
			dataType: 'json',
			data: {
				wxid: wxid,
				openid: openid,
				jid: jid,
				shareWxid: shareWxid,
				shareOpenid: shareOpenid,
				sign: sign,
				gameType: gameType,
				score: score
			},
			success: function(res)
			{
				//alert(res.content);
			},
			error: function(res)
			{
				
			}
		});
	}
}

game.state.add('boot',gameBoot);
game.state.add('preload',gamePreload);
game.state.add('start',gameStart);
game.state.add('over',gameOver);
game.state.start('boot');
s